﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using CTIClassLibrary;

namespace CTIPay
{
    public partial class FrmChinaPnr : Form
    {
        public FrmChinaPnr()
        {
            InitializeComponent();
        }

        private void btnPay_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            CHINAPNRLib.NetpayClientClass cpnr = new CHINAPNRLib.NetpayClientClass();

            string ChkValue;
            string msgData = "";
            string Version = "20";//版本号
            string MerId = "410010";
            string OrdId = txtOrderID.Text.Trim();
            string TransAmt = "0.01";
            string MerDate = dt.ToString("yyyyMMdd");
            string TransType = "支付";//支付
            string GateId = "T1";//建行
            string MerPriv = "";
            string RetUrl = "http://221.232.148.26:3128/CTIPayService/CallBackCPNR.aspx";

            //订单签名
            msgData = Version + MerId + OrdId + TransAmt + MerDate + TransType + GateId + MerPriv + RetUrl;
            byte[] msgBytes = Encoding.Default.GetBytes(msgData);
            ChkValue = cpnr.SignMsg(MerId, "MerPrK410010.key", msgData, msgBytes.Length);

            this.rtbRequest.Text += "SignMsg code is " + ChkValue + System.Environment.NewLine;

            string CardInfo;
            string CardNo = "4367481111111111";
            string ValidDate = "0214";
            string Cvv2 = "123";
            string IdType = "00";//00－身份证
            string IdNo = "352224198204120013";
            string Name = "陈起劲";

            //卡信息加密
            string encMsg = CardNo + "|" + ValidDate + "|" + Cvv2 + "|" + IdType + "|" + IdNo + "|" + Name;
            CardInfo = cpnr.EncMsg("PgPubk.key", encMsg, encMsg.Length);

            this.rtbRequest.Text += "EncMsg code is " + CardInfo + System.Environment.NewLine;

            //支付测试
            string url = "http://test.chinapnr.com/telepayGet/TransGet?Version={0}&MerId={1}&OrdId={2}&TransAmt={3}&MerDate={4}&TransType={5}&GateId={6}&MerPriv={7}&RetUrl={8}&CardInfo={9}&ChkValue={10}";
            url = string.Format(url, Version, MerId, OrdId, TransAmt, MerDate, TransType, GateId, MerPriv, RetUrl, CardInfo, ChkValue);
            this.rtbRequest.Text += url + System.Environment.NewLine;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse res = (HttpWebResponse)request.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream());
            string xml = sr.ReadToEnd();
            this.rtbResponse.Text += xml + System.Environment.NewLine;

            //记录至数据库
            //Common.DB.OnLog += new NBearLite.LogHandler(DB_OnLog);
            string respCode = Common.GetXMLElementValue(xml, "respCode");

            try
            {
                Common.DB.Insert(Tables.t_CPNR_Transfer)
                    .AddColumn(Tables.t_CPNR_Transfer.CardInfo, CardInfo)
                    .AddColumn(Tables.t_CPNR_Transfer.ChkValue, ChkValue)
                    .AddColumn(Tables.t_CPNR_Transfer.GateId, GateId)
                    .AddColumn(Tables.t_CPNR_Transfer.MerDate, MerDate)
                    .AddColumn(Tables.t_CPNR_Transfer.MerId, MerId)
                    .AddColumn(Tables.t_CPNR_Transfer.MerPriv, MerPriv)
                    .AddColumn(Tables.t_CPNR_Transfer.OrdId, OrdId)
                    .AddColumn(Tables.t_CPNR_Transfer.RespCode, respCode)
                    .AddColumn(Tables.t_CPNR_Transfer.RetUrl, RetUrl)
                    //.AddColumn(Tables.t_CPNR_Transfer.SysDate, "")
                    //.AddColumn(Tables.t_CPNR_Transfer.SysSeqId, "")
                    .AddColumn(Tables.t_CPNR_Transfer.TransAmt, TransAmt)
                    //.AddColumn(Tables.t_CPNR_Transfer.transStat, "")
                    .AddColumn(Tables.t_CPNR_Transfer.TransType, TransType)
                    .AddColumn(Tables.t_CPNR_Transfer.version, Version)
                    .Execute();
            }
            catch(Exception ee)
            {
                this.rtbResponse.Text += ee.Message;
            }
        }

        void DB_OnLog(string logMsg)
        {
            this.rtbRequest.Text += logMsg + System.Environment.NewLine;
        }
    }
}
